﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using ERPStore.Logging;

namespace ERPStore.Tests
{
	internal class TestLogger : ILogger
	{
		public void Info(string message)
		{
			message = GetPrefix("Info") + message;
			System.Diagnostics.Debug.WriteLine(message);
		}

		public void Info(string message, params object[] prms)
		{
			message = GetPrefix("Info") + message;
			System.Diagnostics.Debug.WriteLine(message, prms);
		}

		public void Notification(string message)
		{
			message = GetPrefix("Notification") + message;
			System.Diagnostics.Debug.WriteLine(message);
		}

		public void Notification(string message, params object[] prms)
		{
			message = GetPrefix("Notification") + message;
			System.Diagnostics.Debug.WriteLine(message, prms);
		}

		public void Warn(string message)
		{
			message = GetPrefix("Warning") + message;
			System.Diagnostics.Debug.WriteLine(message);
		}

		public void Warn(string message, params object[] prms)
		{
			message = GetPrefix("Warning") + message;
			System.Diagnostics.Debug.WriteLine(message, prms);
		}

		public void Debug(string message)
		{
			message = GetPrefix("Debug") + message;
			System.Diagnostics.Debug.WriteLine(message);
		}

		public void Debug(string message, params object[] prms)
		{
			message = GetPrefix("Debug") + message;
			System.Diagnostics.Debug.WriteLine(string.Format(message, prms));
		}

		public void Error(string message)
		{
			message = GetPrefix("Error") + message;
			System.Diagnostics.Debug.WriteLine(message);
		}

		public void Error(string message, params object[] prms)
		{
			message = GetPrefix("Error") + message;
			System.Diagnostics.Debug.WriteLine(message, prms);
		}

		public void Error(Exception x)
		{
			var message = GetPrefix("Error") + x.ToString();
			System.Diagnostics.Debug.WriteLine(x.ToString());
		}

		public void Fatal(string message)
		{
			message = GetPrefix("Fatal") + message;
			System.Diagnostics.Debug.WriteLine(message);
		}

		public void Fatal(string message, params object[] prms)
		{
			message = GetPrefix("Fatal") + message;
			System.Diagnostics.Debug.WriteLine(message, prms);
		}

		public void Fatal(Exception x)
		{
			string message = GetPrefix("Fatal") + x.ToString();
			System.Diagnostics.Debug.WriteLine(message);
		}

		private System.Diagnostics.TextWriterTraceListener m_Out;

		public System.IO.TextWriter Out
		{
			get
			{
				if (m_Out == null)
				{
					m_Out = new System.Diagnostics.TextWriterTraceListener();// new ConsoleTextWriter();
				}
				return m_Out.Writer;
			}
		}

		public void Watch(string title, System.Threading.ThreadStart method)
		{
			var watch = Stopwatch.StartNew();
			watch.Start();
			method.Invoke();
			watch.Stop();
			Info("{0} = {1}ms", title, watch.ElapsedMilliseconds);
		}

		string GetPrefix(string prf)
		{
			var thread = System.Threading.Thread.CurrentThread;
			return string.Format("{0:yyyyMMdd}|{0:HH}H{0:mm}:{0:ss}.{0:ffff}\t|{1}({2})\t|\t{3} :", DateTime.Now, thread.Name, thread.ManagedThreadId, prf);
		}
	}
}
